package com.atguigu.day03;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Flink11_Transform_Process {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //TODO 3.在keyBy之前使用Process 实现flatMap功能，将每一个单词按照空格切分然后Tuple2元组
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOneDStream = streamSource.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1));
                }
            }
        });

        //4.将相同单词的数据聚合到一块
        KeyedStream<Tuple2<String, Integer>, String> keyedStream = wordToOneDStream.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
            @Override
            public String getKey(Tuple2<String, Integer> value) throws Exception {
                return value.f0;
            }
        });

        //TODO 5.在KeyBy之后使用Process 实现sum的功能，对value做累加
        keyedStream.process(new KeyedProcessFunction<String, Tuple2<String, Integer>, Tuple2<String, Integer>>() {
            //自定义一个累加器，保存上一次计算的结果
//            private Integer lastSum = 0;
            private  HashMap<String, Integer> lastSum = new HashMap<>();

            @Override
            public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {

                if (lastSum.containsKey(value.f0)){
                    //累加器中有当前的key的结果
                    Integer lastValue = lastSum.get(value.f0);
                    //将当前的值与上一次累加后的值累加并更新至累加器中
                    lastValue = lastValue + value.f1;
                    lastSum.put(value.f0, lastValue);
                    out.collect(Tuple2.of(value.f0,lastValue));
                }else {
                    //累加器中没有当前的key，则直接将当前数据保存至累加器中
                    lastSum.put(value.f0, value.f1);
                    out.collect(Tuple2.of(value.f0,value.f1));
                }
            }
        }).print();

        env.execute();
    }
}
